﻿using System;
using System.Collections;

[Serializable]
public class ShoppingCart
{
    public Hashtable _CartItems = new Hashtable();

    // Return all the items from the Shopping Cart
    public ICollection CartItems
    {
        get { return _CartItems.Values; }
    }

    // The sum total of the prices
    public decimal Total
    {
        get
        {
            decimal sum = 0;
            foreach (CartItem item in _CartItems.Values)
                sum += item.Price * item.Quantity;
            return sum;
        }
    }

    // Add a new item to the shopping cart
    public void AddItem(int ID, string Name, decimal Price)
    {
        CartItem item = (CartItem)_CartItems[ID];
        if (item == null)
            _CartItems.Add(ID, new CartItem(ID, Name, Price));
        else
        {
            item.Quantity++;
            _CartItems[ID] = item;
        }
    }

    // Remove an item from the shopping cart
    public void RemoveItem(int ID)
    {
        CartItem item = (CartItem)_CartItems[ID];
        if (item == null)
            return;
        item.Quantity--;
        if (item.Quantity == 0)
            _CartItems.Remove(ID);
        else
            _CartItems[ID] = item;
    }

}
